var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
	upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
}
function fy_dialog_close() {
	//window.parent.$(".mask").remove();
	window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
}

function getUpFields(name) {
	return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
	return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}
function getUpAccountFieldValue(name) {
	var field = getUpFields(name).eq(0);
	return {
		displayValue : field.is("span") ? field.text() : field.val(),
		value : eval("(" + field.parent("span").attr("fieldVal") + ")").value
	};
}

function addOrDelRepeat(name, newLength) {
	var fields = getUpFieldsByFieldval(name);
	var oldLength = fields.length;
	var tbody = getUpFieldsByFieldval(name).eq(0).parent();
	while (!tbody.is("tbody")) {
		tbody = tbody.parent();
	}
	for (var i = 0, length = Math.abs(newLength - oldLength); i < length; i++) {
		if (newLength > oldLength) {
			upWindow.fy_addEmpty(tbody.children().last());
		} else {
			upWindow.fy_delCurrentRepeat(tbody.children().last());
		}
	}
}

function getUpFieldValue(field) {
	var fieldVal = field.attr("fieldVal");
	var editTag = field.hasClass(editClass);
	var browseTag = field.hasClass(browseClass);
	var addTag = field.hasClass(addClass);
	if (fieldVal != null && typeof(fieldVal) != 'undefined') {
		fieldVal = eval("(" + fieldVal + ")");
	} else {
		return null;
	}
	var obj = new Object();
	obj.displayName = fieldVal.displayName;
	obj.name = fieldVal.name;
	var tag = true;
	switch (fieldVal.inputType) {
		case "text" :
		case "textarea" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			} else if (browseTag) {
				obj.value = fieldVal.value;
			} else if (addTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			}
			break;
		case "checkbox" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			} else if (browseTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			}
			break;
		case "radio" :
			if (editTag) {
				obj.value = field.find(":radio:checked").attr("val4cal");
			} else if (browseTag) {
				obj.value = field.find(":radio:checked").attr("val4cal");
			}
			break;
		case "select" :
			if (editTag) {
				obj.value = field
						.find("#" + fieldVal.name + " option:selected")
						.attr("val4cal");
			} else if (browseTag) {
				obj.value = field.find("#" + fieldVal.name).attr("val4cal");
			}
			break;
		case "date" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
				if (obj.value != "") {
					obj.value = obj.value + " 00:00";
				}
			} else if (browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "datetime" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			} else if (browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "flowdealoption" :
			if (editTag) {
				obj.value = fieldVal.value;
			} else if (browseTag) {
				obj.value = fieldVal.value;
			} else if (addTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "lable" :
			if (editTag) {
				obj.value = fieldVal.value;
			} else if (browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "relationform" :
			if (editTag) {
				obj.value = fieldVal.value;
			} else if (browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "relation" :
			if (editTag || browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "project" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			} else if (browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "member" :
		case "multimember" :
		case "account" :
		case "multiaccount" :
		case "department" :
		case "multidepartment" :
		case "post" :
		case "multipost" :
		case "level" :
		case "multilevel" :
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name + "_txt").val();
				obj.id = field.find("#" + fieldVal.name).val().split('|')[1];
			} else if (browseTag) {
				obj.value = field.find("#" + fieldVal.name).text();
			}
			break;
		case "attachment" :
		case "image" :
		case "document" :
			if (editTag || browseTag) {
				var attNames = "";
				var tempField = field.find("#" + fieldVal.name);
				var tempFileId = "";
				if (tempField[0].tagName.toLowerCase() == "input") {
					tempFileId = tempField.val();
				} else if (tempField[0].tagName.toLowerCase() == "span") {
					tempFileId = tempField[0].innerHTML;
				}
				var atts = upWindow.getAttBySubreference(tempFileId);
				for (var j = 0; j < atts.length; j++) {
					attNames += atts[j].filename + ",";
				}
				if (attNames != "") {
					attNames = attNames.substr(0, attNames.length - 1);
				}
				obj.value = attNames;
			}
			break;
		case "outwrite" :
			if (editTag || browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "externalwrite-ahead" :
			if (editTag || browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "exchangetask" :
			if (editTag || browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		case "querytask" :
			if (editTag || browseTag) {
				obj.value = fieldVal.value;
			}
			break;
		default :
			tag = false;
	}
	if (obj.value != undefined) {
		obj.value = obj.value.replace("\r\n", ";");
		obj.value = obj.value.replace("\n", ";");
	}
	return obj;
}
function setUpFieldValue(name, value) {
	upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
			.val(	value);
	upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
			.trigger("blur");
}

function setUpFieldValue2(field, value, displayValue, index) {
	var fieldVal = field.attr("fieldVal");
	var editTag = field.hasClass(editClass);
	var addTag = field.hasClass(addClass);
	value = trim(value);
	displayValue = trim(displayValue);
	if (fieldVal != null && typeof(fieldVal) != 'undefined') {
		fieldVal = eval("(" + fieldVal + ")");
	} else {
		return null;
	}

	var tag = true;
	switch (fieldVal.inputType) {
		case "text" :
		case "textarea" :
			if (editTag) {
				var inField = field.find("#" + fieldVal.name);
				inField.val(value);
				inField.trigger("blur");
			} else if (addTag) {
				var inField = field.find("#" + fieldVal.name);
				inField.val(value);
				inField.trigger("blur");
			}
			break;
		case "checkbox" :
			if (editTag) {
				field.find("#" + fieldVal.name).attr("checked", true);
				field.find("#" + fieldVal.name).trigger("click");
			}
			break;
		case "radio" :
			if (editTag) {
				field.find(":radio").eq(value).attr("checked", true);
				field.find(":radio").eq(value).trigger("click");
			}
			break;
		case "select" :
			if (editTag) {
				var options = field.find("#" + fieldVal.name + " option");
				for (var k = 0; k < options.length; k++) {
					if (options.eq(k).text() == value)
						options.eq(k).attr("selected", true);
				}
				field.find("#" + fieldVal.name).trigger("change");
			}
			break;
		case "date" :
			if (editTag) {
				field.find("#" + fieldVal.name).val(value);
				field.find("#" + fieldVal.name).trigger("blur");
			}
			break;
		case "datetime" :
			if (editTag) {
				field.find("#" + fieldVal.name).val(value);
				field.find("#" + fieldVal.name).trigger("blur");
			}
			break;
		case "member" :
		case "multimember" :
		case "account" :
		case "multiaccount" :
		case "department" :
		case "multidepartment" :
		case "post" :
		case "multipost" :
		case "level" :
		case "multilevel" :
			if (editTag) {
				field.find("#" + fieldVal.name).val(value);
			}
			break;
	}
}

/**
 * 去除左右空格
 */
function trim(str) {
	return (str) ? (typeof str == "string" ? str.replace(/^\s*/, "").replace(
			/\s*$/, "") : str) : "";
}

var url = './oa2hrController.do';
var method = 'staffRedeployment';
var depArray = ['dep1', 'dep2', 'dep3', 'dep4'];
var postSelectID = 'postSelect';
var postRankMap = {};

var depSelectValue = null;

var _ajax = function(params, url, operator) {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("lodingDiv").style.display = "none";
			operator(xmlhttp);
		}
	};
	document.getElementById("lodingDiv").style.display = "";
	xmlhttp.open("get", url + "?" + params + "&temp=" + Math.random(), true);
	xmlhttp.send();
};

function createOption(msg,key){
	var op = document.createElement("option");
	op.value = key;
	op.text = msg;
	return op;
}

var clearSelect = function(selectID, hide) {
	var selectNode = document.getElementById(selectID);
	while (selectNode.options.length > 0) {
		selectNode.options.remove(0);
	}
	document.getElementById(selectID).options.add(createOption("", ""));
	if (hide === true) {
		selectNode.style.display = 'none';
	} else {
		selectNode.style.display = '';
	}
};
var getSelectText = function(selectID) {
	return document.getElementById(selectID).selectedIndex == -1
			? ""
			: document.getElementById(selectID).options[document
					.getElementById(selectID).selectedIndex].innerText;
};
var myHandler = {
	onComanySelectValueChange : function() {

		if (document.getElementById("msgOption")) {
			document.getElementById("companySelect").removeChild(document
							.getElementById("msgOption"));
		}
		clearSelect(depArray[0]);
		clearSelect(depArray[1], true);
		clearSelect(depArray[2], true);
		clearSelect(depArray[3], true);
		clearSelect(postSelectID);
		var corpID = document.getElementById("companySelect").value;
		var params = "method=" + method + "&operate=1&id=" + corpID;
		_ajax(params, url, function(xmlhttp) {
					text = xmlhttp.responseText;
					if (text == '') {
						alert('获取部门信息异常，请重试或者联系信息部');
					} else {
						var result = eval('(' + text + ')');
						for (var i = 0; i < result.length; i++) {
							var depNode = eval('(' + result[i] + ')');
							document.getElementById(depArray[0]).options
									.add(createOption(depNode.name, depNode.id));
						}

					}
				});
	},
	onDepSelectValueChange : function(el) {

		var depID = el.value;

		// if (!depSelectValue ) {
		// depSelectValue = depID;
		// } else {
		// if (depSelectValue == depID) {
		// return;
		// }
		// }

		clearSelect(postSelectID);
		var selectIndex = -1;
		if (el.id == depArray[0]) {
			clearSelect(depArray[1], true);
			clearSelect(depArray[2], true);
			clearSelect(depArray[3], true);
			selectIndex = 0;
		} else if (el.id == depArray[1]) {
			clearSelect(depArray[2], true);
			clearSelect(depArray[3], true);
			selectIndex = 1;
		} else if (el.id == depArray[2]) {
			clearSelect(depArray[3], true);
			selectIndex = 2;
		} else if (el.id == depArray[3]) {
			selectIndex = 3;
		}

		if (!depID || depID == "") {
			if (selectIndex > 0) {
				depID = document.getElementById(depArray[selectIndex - 1]).value;
				clearSelect(depArray[selectIndex], false);
				selectIndex = selectIndex - 1;
			} else {
				return;
			}
		}
		var params = "method=" + method + "&operate=2&id=" + depID;
		_ajax(params, url, function(xmlhttp) {
			text = xmlhttp.responseText;
			if (text == '') {
				alert('获取部门信息异常，请重试或者联系信息部');
			} else {
				var result = eval('(' + text + ')');
				if (result.children && result.children != "" && selectIndex < 3) {
					for (var i = 0; i < result.children.length; i++) {
						var depNode = eval('(' + result.children[i] + ')');
						document.getElementById(depArray[selectIndex + 1]).options
								.add(createOption(depNode.name, depNode.id));
						document.getElementById(depArray[selectIndex + 1]).style.display = '';
					}
				}

				postRankMap = {};
				if (result.posts && result.posts != "") {
					for (var i = 0; i < result.posts.length; i++) {
						var postNode = eval('(' + result.posts[i] + ')');
						document.getElementById(postSelectID).options
								.add(createOption(postNode.name, postNode.id));
						postRankMap[postNode.id] = (postNode.rank
								? postNode.rank
								: "");
					}
				}
			}
		});

	},
	onOKButtonClick : function() {

		var newJob = document.getElementById("postSelect").value;
		if (!newJob || newJob == "") {
			alert("请选择人员的调入单位、部门和岗位");
			return;
		}
		var memberCode = document.getElementById("memberCode").value;

		if (!memberCode || memberCode == "") {
			alert("人员工号不能为空");
			return;
		}

		var params = "method=" + method + "&operate=4&id=" + memberCode;
		_ajax(params, url, function(xmlhttp) {
			text = xmlhttp.responseText;
			if (text == '') {
				alert('HR系统中不存在该工号对应的人员');
			} else if (text == '{}') {
				alert('系统异常，请重试');
			} else {
				var memberJSON = eval('(' + text + ')');
				setUpFieldValue2(getUpFieldsByFieldval("人员编号_").eq(0),
						memberJSON.memberCode);
				setUpFieldValue2(getUpFieldsByFieldval("调动人姓名_").eq(0),
						memberJSON.name);
				setUpFieldValue2(getUpFieldsByFieldval("进入福耀时间_").eq(0),
						memberJSON.joinTime);
				setUpFieldValue2(getUpFieldsByFieldval("聘用学历_").eq(0),
						memberJSON.education);
				setUpFieldValue2(getUpFieldsByFieldval("调出单位岗位_").eq(0),
						memberJSON.nowPost);
				setUpFieldValue2(getUpFieldsByFieldval("调出单位职级_").eq(0),
						memberJSON.nowLevel ? memberJSON.nowLevel : "");
				setUpFieldValue2(getUpFieldsByFieldval("调出单位车间科室_").eq(0),
						memberJSON.dep2 ? memberJSON.dep2 : "");
				// 不存在车间或科室为0存在为1
				setUpFieldValue2(getUpFieldsByFieldval("存在调出车间或科室").eq(0),
						memberJSON.dep2 && memberJSON.dep2 != "" ? '1' : '0');
				setUpFieldValue2(getUpFieldsByFieldval("调出单位部门工厂_").eq(0),
						memberJSON.dep1);
				setUpFieldValue2(getUpFieldsByFieldval("调出制造部_").eq(0),
						memberJSON.corp);

				setUpFieldValue2(getUpFieldsByFieldval("调入单位岗位_").eq(0),
						getSelectText(postSelectID));
				setUpFieldValue2(
						getUpFieldsByFieldval("调入单位职级_").eq(0),
						postRankMap[document.getElementById(postSelectID).value]
								? postRankMap[document
										.getElementById(postSelectID).value]
								: "");
				var dep2 = getSelectText(depArray[1]);
				var dep3 = getSelectText(depArray[2]);
				var dep4 = getSelectText(depArray[3]);
				var childDep = "";
				if (dep2 && dep2 != null) {
					childDep = dep2;
					if (dep3 && dep3 != null) {
						childDep += "/" + dep3;
						if (dep4 && dep4 != null) {
							childDep += "/" + dep4;
						}
					}
				}
				setUpFieldValue2(getUpFieldsByFieldval("调入单位车间科室_").eq(0),
						childDep);
				setUpFieldValue2(getUpFieldsByFieldval("调入单位部门工厂_").eq(0),
						getSelectText(depArray[0]));
				setUpFieldValue2(
						getUpFieldsByFieldval("调入单位选择").eq(0),
						corpMap['key_'
								+ document.getElementById("companySelect").value]);

				setUpFieldValue2(
						getUpFieldsByFieldval("调入单位选择_").eq(0),
						corpMap1['key_'
								+ document.getElementById("companySelect").value][1]);

				setUpFieldValue2(getUpFieldsByFieldval("调入制造部_").eq(0),
						(getSelectText('companySelect') == "请选择"
								? ""
								: (getSelectText('companySelect'))));
				fy_dialog_close();
			}
		});

	}
};
